home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 90 / PIWD90.iso / mac / contents / flash_gaming / tutorial_files / Pages70-80 / question animation.swf.swf / scripts / FComboBoxSymbol.as < prev    next >
Text File  |  2003-12-22  |  14KB  |  479 lines

  1. function FComboBoxClass()
  2. {
  3.    _global._popUpLevel = _global._popUpLevel != undefined ? _global._popUpLevel + 1 : 20000;
  4.    this.superHolder = _root.createEmptyMovieClip("superHolder" + _popUpLevel,_popUpLevel);
  5.    var testContainer = this.superHolder.createEmptyMovieClip("testCont",20000);
  6.    var testBox = testContainer.attachMovie("FBoundingBoxSymbol","boundingBox_mc",0);
  7.    if(testBox._name == undefined)
  8.    {
  9.       this.superHolder.removeMovieClip();
  10.       this.superHolder = this._parent.createEmptyMovieClip("superHolder" + _popUpLevel,_popUpLevel);
  11.    }
  12.    else
  13.    {
  14.       testContainer.removeMovieClip();
  15.    }
  16.    if(this.rowCount == undefined)
  17.    {
  18.       this.rowCount = 8;
  19.       this.editable = false;
  20.    }
  21.    this.itemSymbol = "FComboBoxItemSymbol";
  22.    this.init();
  23.    this.permaScrollBar = false;
  24.    this.proxyBox_mc.gotoAndStop(1);
  25.    this.width = this._width;
  26.    this.height = this.proxyBox_mc._height * this._yscale / 100;
  27.    var i = 0;
  28.    while(i < this.labels.length)
  29.    {
  30.       this.addItem(this.labels[i],this.data[i]);
  31.       i++;
  32.    }
  33.    this.lastSelected = 0;
  34.    this.selectItem(0);
  35.    this._xscale = this._yscale = 100;
  36.    this.opened = false;
  37.    this.setSize(this.width);
  38.    this.highlightTop(false);
  39.    if(this.changeHandler.length > 0)
  40.    {
  41.       this.setChangeHandler(this.changeHandler);
  42.    }
  43.    this.onUnload = function()
  44.    {
  45.       this.superHolder.removeMovieClip();
  46.    };
  47.    this.setSelectedIndex(0,false);
  48.    this.value = "";
  49.    this.focusEnabled = true;
  50.    this.changeFlag = false;
  51. }
  52. FComboBoxClass.prototype = new FScrollSelectListClass();
  53. Object.registerClass("FComboBoxSymbol",FComboBoxClass);
  54. FComboBoxClass.prototype.modelChanged = function(eventObj)
  55. {
  56.    super.modelChanged(eventObj);
  57.    var event = eventObj.event;
  58.    if(event == "addRows" || event == "deleteRows")
  59.    {
  60.       var diff = eventObj.lastRow - eventObj.firstRow + 1;
  61.       var mode = event != "addRows" ? -1 : 1;
  62.       var len = this.getLength();
  63.       var lenBefore = len - mode * diff;
  64.       if(this.rowCount > lenBefore || this.rowCount > len)
  65.       {
  66.          this.invalidate("setSize");
  67.       }
  68.       if(this.getSelectedIndex() == undefined)
  69.       {
  70.          this.setSelectedIndex(0,false);
  71.       }
  72.    }
  73.    else if(event == "updateAll")
  74.    {
  75.       this.invalidate("setSize");
  76.    }
  77. };
  78. FComboBoxClass.prototype.removeAll = function()
  79. {
  80.    if(!this.enable)
  81.    {
  82.       return undefined;
  83.    }
  84.    super.removeAll();
  85.    if(this.editable)
  86.    {
  87.       this.value = "";
  88.    }
  89.    this.invalidate("setSize");
  90. };
  91. FComboBoxClass.prototype.setSize = function(w)
  92. {
  93.    if(w == undefined || typeof w != "number" || w <= 0 || !this.enable)
  94.    {
  95.       return undefined;
  96.    }
  97.    this.proxyBox_mc._width = w;
  98.    this.container_mc.removeMovieClip();
  99.    this.measureItmHgt();
  100.    this.container_mc = this.superHolder.createEmptyMovieClip("container",3);
  101.    this.container_mc.tabChildren = false;
  102.    this.setPopUpLocation(this.container_mc);
  103.    this.container_mc.attachMovie("FBoundingBoxSymbol","boundingBox_mc",0);
  104.    this.boundingBox_mc = this.container_mc.boundingBox_mc;
  105.    this.boundingBox_mc.component = this;
  106.    this.registerSkinElement(this.boundingBox_mc.boundingBox,"background");
  107.    this.proxyBox_mc._height = this.itmHgt;
  108.    this.numDisplayed = Math.min(this.rowCount,this.getLength());
  109.    if(this.numDisplayed < 3)
  110.    {
  111.       this.numDisplayed = Math.min(3,this.getLength());
  112.    }
  113.    this.height = this.numDisplayed * (this.itmHgt - 2) + 2;
  114.    super.setSize(w,this.height);
  115.    this.attachMovie("DownArrow","downArrow",10);
  116.    this.downArrow._y = 0;
  117.    this.downArrow._width = this.itmHgt;
  118.    this.downArrow._height = this.itmHgt;
  119.    this.downArrow._x = this.proxyBox_mc._width - this.downArrow._width;
  120.    this.setEditable(this.editable);
  121.    this.container_mc._visible = this.opened;
  122.    this.highlightTop(false);
  123.    this.fader = this.superHolder.attachMovie("FBoundingBoxSymbol","faderX",4);
  124.    this.registerSkinElement(this.fader.boundingBox,"background");
  125.    this.fader._width = this.width;
  126.    this.fader._height = this.height;
  127.    this.fader._visible = false;
  128. };
  129. FComboBoxClass.prototype.setDataProvider = function(dp)
  130. {
  131.    super.setDataProvider(dp);
  132.    this.invalidate("setSize");
  133.    this.setSelectedIndex(0);
  134. };
  135. FComboBoxClass.prototype.getValue = function()
  136. {
  137.    if(this.editable)
  138.    {
  139.       return this.fLabel_mc.getLabel();
  140.    }
  141.    return super.getValue();
  142. };
  143. FComboBoxClass.prototype.getRowCount = function()
  144. {
  145.    return this.rowCount;
  146. };
  147. FComboBoxClass.prototype.setRowCount = function(count)
  148. {
  149.    this.rowCount = this.getLength() <= count ? count : Math.max(count,3);
  150.    this.setSize(this.width);
  151.    var len = this.getLength();
  152.    if(len - this.getScrollPosition() < this.rowCount)
  153.    {
  154.       this.setScrollPosition(len - Math.min(this.rowCount,len));
  155.       this.invalidate("updateControl");
  156.    }
  157. };
  158. FComboBoxClass.prototype.setEditable = function(editableFlag)
  159. {
  160.    if(!this.enable)
  161.    {
  162.       return undefined;
  163.    }
  164.    this.editable = editableFlag;
  165.    if(!this.editable)
  166.    {
  167.       this.onPress = this.pressHandler;
  168.       this.useHandCursor = false;
  169.       this.trackAsMenu = true;
  170.       this.attachMovie("FComboBoxItemSymbol","fLabel_mc",5,{controller:this,itemNum:-1});
  171.       this.fLabel_mc.onRollOver = undefined;
  172.       this.fLabel_mc.setSize(this.width - this.itmHgt + 1,this.itmHgt);
  173.       this.topLabel = this.getSelectedItem();
  174.       this.fLabel_mc.drawItem(this.topLabel,false);
  175.       this.highlightTop(false);
  176.    }
  177.    else
  178.    {
  179.       this.attachMovie("FLabelSymbol","fLabel_mc",5);
  180.       this.fLabel_txt = this.fLabel_mc.labelField;
  181.       this.fLabel_txt.type = "input";
  182.       this.fLabel_txt._x = 4;
  183.       this.fLabel_txt.onSetFocus = this.onLabelFocus;
  184.       this.fLabel_mc.setSize(this.width - this.itmHgt - 3);
  185.       delete this.onPress;
  186.       this.fLabel_txt.onKillFocus = function()
  187.       {
  188.          this._parent._parent.myOnKillFocus();
  189.       };
  190.       this.fLabel_mc.setLabel(this.value);
  191.       this.fLabel_txt.onChanged = function()
  192.       {
  193.          this._parent._parent.findInputText();
  194.       };
  195.       this.downArrow.onPress = this.buttonPressHandler;
  196.       this.downArrow.useHandCursor = false;
  197.       this.downArrow.trackAsMenu = true;
  198.    }
  199. };
  200. FComboBoxClass.prototype.setEnabled = function(enabledFlag)
  201. {
  202.    enabledFlag = !(enabledFlag == undefined || typeof enabledFlag != "boolean") ? enabledFlag : true;
  203.    super.setEnabled(enabledFlag);
  204.    this.registerSkinElement(this.boundingBox_mc.boundingBox,"background");
  205.    this.proxyBox_mc.gotoAndStop(!this.enable ? "disabled" : "enabled");
  206.    this.downArrow.gotoAndStop(!this.enable ? 3 : 1);
  207.    if(this.editable)
  208.    {
  209.       this.fLabel_txt.type = !enabledFlag ? "dynamic" : "input";
  210.       this.fLabel_txt.selectable = enabledFlag;
  211.    }
  212.    else if(enabledFlag)
  213.    {
  214.       this.fLabel_mc.drawItem(this.topLabel,false);
  215.       this.setSelectedIndex(this.getSelectedIndex(),false);
  216.    }
  217.    this.fLabel_mc.setEnabled(this.enable);
  218.    this.fLabel_txt.onSetFocus = !enabledFlag ? undefined : this.onLabelFocus;
  219. };
  220. FComboBoxClass.prototype.setSelectedIndex = function(index, flag)
  221. {
  222.    super.setSelectedIndex(index,flag);
  223.    if(!this.editable)
  224.    {
  225.       this.topLabel = this.getSelectedItem();
  226.       this.fLabel_mc.drawItem(this.topLabel,false);
  227.    }
  228.    else
  229.    {
  230.       this.value = flag == undefined ? this.getSelectedItem().label : "";
  231.       this.fLabel_mc.setLabel(this.value);
  232.    }
  233.    this.invalidate("updateControl");
  234. };
  235. FComboBoxClass.prototype.setValue = function(value)
  236. {
  237.    if(this.editable)
  238.    {
  239.       this.fLabel_mc.setLabel(value);
  240.       this.value = value;
  241.    }
  242. };
  243. FComboBoxClass.prototype.pressHandler = function()
  244. {
  245.    this.focusRect.removeMovieClip();
  246.    if(this.enable)
  247.    {
  248.       if(!this.opened)
  249.       {
  250.          this.onMouseUp = this.releaseHandler;
  251.       }
  252.       else
  253.       {
  254.          this.onMouseUp = undefined;
  255.       }
  256.       this.changeFlag = false;
  257.       if(!this.focused)
  258.       {
  259.          this.pressFocus();
  260.          this.clickFilter = !this.editable ? true : false;
  261.       }
  262.       if(!this.clickFilter)
  263.       {
  264.          this.openOrClose(!this.opened);
  265.       }
  266.       else
  267.       {
  268.          this.clickFilter = false;
  269.       }
  270.    }
  271. };
  272. FComboBoxClass.prototype.clickHandler = function(itmNum)
  273. {
  274.    if(!this.focused)
  275.    {
  276.       if(this.editable)
  277.       {
  278.          this.fLabel_txt.onKillFocus = undefined;
  279.       }
  280.       this.pressFocus();
  281.    }
  282.    super.clickHandler(itmNum);
  283.    this.selectionHandler(itmNum);
  284.    this.onMouseUp = this.releaseHandler;
  285. };
  286. FComboBoxClass.prototype.highlightTop = function(flag)
  287. {
  288.    if(!this.editable)
  289.    {
  290.       this.fLabel_mc.drawItem(this.topLabel,flag);
  291.    }
  292. };
  293. FComboBoxClass.prototype.myOnSetFocus = function()
  294. {
  295.    super.myOnSetFocus();
  296.    this.fLabel_mc.highlight_mc.gotoAndStop("enabled");
  297.    this.highlightTop(true);
  298. };
  299. FComboBoxClass.prototype.drawFocusRect = function()
  300. {
  301.    this.drawRect(-2,-2,this.width + 4,this._height + 4);
  302. };
  303. FComboBoxClass.prototype.myOnKillFocus = function()
  304. {
  305.    if(Selection.getFocus().indexOf("labelField") != -1)
  306.    {
  307.       return undefined;
  308.    }
  309.    super.myOnKillFocus();
  310.    delete this.fLabel_txt.onKeyDown;
  311.    this.openOrClose(false);
  312.    this.highlightTop(false);
  313. };
  314. FComboBoxClass.prototype.setPopUpLocation = function(mcRef)
  315. {
  316.    mcRef._x = this._x;
  317.    var point = {x:this._x,y:this._y + this.proxyBox_mc._height};
  318.    this._parent.localToGlobal(point);
  319.    mcRef._parent.globalToLocal(point);
  320.    mcRef._x = point.x;
  321.    mcRef._y = point.y;
  322.    if(this.height + mcRef._y >= Stage.height)
  323.    {
  324.       this.upward = true;
  325.       mcRef._y = point.y - this.height - this.proxyBox_mc._height;
  326.    }
  327.    else
  328.    {
  329.       this.upward = false;
  330.    }
  331. };
  332. FComboBoxClass.prototype.openOrClose = function(flag)
  333. {
  334.    if(this.getLength() == 0)
  335.    {
  336.       return undefined;
  337.    }
  338.    this.setPopUpLocation(this.container_mc);
  339.    if(this.lastSelected != -1 && (this.lastSelected < this.topDisplayed || this.lastSelected > this.topDisplayed + this.numDisplayed))
  340.    {
  341.       super.moveSelBy(this.lastSelected - this.getSelectedIndex());
  342.    }
  343.    !flag ? this.downArrow.gotoAndStop(1) : this.downArrow.gotoAndStop(2);
  344.    if(flag == this.opened)
  345.    {
  346.       return undefined;
  347.    }
  348.    this.highlightTop(!flag);
  349.    this.fadeRate = this.styleTable.popUpFade.value;
  350.    if(!flag || this.fadeRate == undefined || this.fadeRate == 0)
  351.    {
  352.       this.opened = this.container_mc._visible = flag;
  353.       return undefined;
  354.    }
  355.    this.setPopUpLocation(this.fader);
  356.    this.time = 0;
  357.    this.const = 85 / Math.sqrt(this.fadeRate);
  358.    this.fader._alpha = 85;
  359.    this.container_mc._visible = this.fader._visible = true;
  360.    this.onEnterFrame = function()
  361.    {
  362.       this.fader._alpha = 100 - (this.const * Math.sqrt(++this.time) + 15);
  363.       if(this.time >= this.fadeRate)
  364.       {
  365.          this.fader._visible = false;
  366.          delete this.onEnterFrame;
  367.          this.opened = true;
  368.       }
  369.    };
  370. };
  371. FComboBoxClass.prototype.fireChange = function()
  372. {
  373.    this.lastSelected = this.getSelectedIndex();
  374.    if(!this.editable)
  375.    {
  376.       this.topLabel = this.getSelectedItem();
  377.       this.fLabel_mc.drawItem(this.topLabel,true);
  378.    }
  379.    else
  380.    {
  381.       this.value = this.getSelectedItem().label;
  382.       this.fLabel_mc.setLabel(this.value);
  383.    }
  384.    this.executeCallback();
  385. };
  386. FComboBoxClass.prototype.releaseHandler = function()
  387. {
  388.    var onCombo = this.boundingBox_mc.hitTest(_root._xmouse,_root._ymouse);
  389.    if(this.changeFlag)
  390.    {
  391.       if(onCombo)
  392.       {
  393.          this.fireChange();
  394.       }
  395.       this.openOrClose(!this.opened);
  396.    }
  397.    else if(onCombo)
  398.    {
  399.       this.openOrClose(false);
  400.    }
  401.    else
  402.    {
  403.       this.onMouseDown = function()
  404.       {
  405.          if(!this.boundingBox_mc.hitTest(_root._xmouse,_root._ymouse) && !this.hitTest(_root._xmouse,_root._ymouse))
  406.          {
  407.             this.onMouseDown = undefined;
  408.             this.openOrClose(false);
  409.          }
  410.       };
  411.    }
  412.    this.changeFlag = false;
  413.    this.onMouseUp = undefined;
  414.    clearInterval(this.dragScrolling);
  415.    this.dragScrolling = undefined;
  416. };
  417. FComboBoxClass.prototype.moveSelBy = function(itemNum)
  418. {
  419.    if(itemNum != 0)
  420.    {
  421.       super.moveSelBy(itemNum);
  422.       if(this.editable)
  423.       {
  424.          this.setValue(this.getSelectedItem().label);
  425.       }
  426.       if(!this.opened)
  427.       {
  428.          if(this.changeFlag && !this.isSelected(this.lastSelected))
  429.          {
  430.             this.fireChange();
  431.          }
  432.       }
  433.    }
  434. };
  435. FComboBoxClass.prototype.myOnKeyDown = function()
  436. {
  437.    if(!this.focused)
  438.    {
  439.       return undefined;
  440.    }
  441.    if(this.editable && Key.isDown(13))
  442.    {
  443.       this.setValue(this.fLabel_mc.getLabel());
  444.       this.executeCallback();
  445.       this.openOrClose(false);
  446.    }
  447.    else if((Key.isDown(13) || Key.isDown(32) && !this.editable) && this.opened)
  448.    {
  449.       if(this.getSelectedIndex() != this.lastSelected)
  450.       {
  451.          this.fireChange();
  452.       }
  453.       this.openOrClose(false);
  454.       this.fLabel_txt.hscroll = 0;
  455.    }
  456.    super.myOnKeyDown();
  457. };
  458. FComboBoxClass.prototype.findInputText = function()
  459. {
  460.    if(!this.editable)
  461.    {
  462.       super.findInputText();
  463.    }
  464. };
  465. FComboBoxClass.prototype.onLabelFocus = function()
  466. {
  467.    this._parent._parent.tabFocused = false;
  468.    this._parent._parent.focused = true;
  469.    this.onKeyDown = function()
  470.    {
  471.       this._parent._parent.myOnKeyDown();
  472.    };
  473.    Key.addListener(this);
  474. };
  475. FComboBoxClass.prototype.buttonPressHandler = function()
  476. {
  477.    this._parent.pressHandler();
  478. };
  479.